package com.xuliucar.car.ui.fragment

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.os.Bundle
import com.cangmaomao.lib.action.f_myNote
import com.cangmaomao.lib.action.f_openPen
import com.cangmaomao.lib.action.f_penManage
import com.cangmaomao.lib.base.BaseFragment
import com.cangmaomao.lib.event.AppEvent
import com.cangmaomao.lib.utils.StatusBarUtil
import com.cangmaomao.lib.utils.toast
import com.xuliucar.car.R
import com.xuliucar.car.contract.MainContract
import kotlinx.android.synthetic.main.f_main.*
import org.greenrobot.eventbus.EventBus

class MainFragment : BaseFragment<MainContract.MainPresenter>() {

    override val contentViewLayoutID: Int
        get() = R.layout.f_main

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setStatusBarColor(_mActivity, R.color.c_w)

        //获取蓝牙适配器
        val adapter = BluetoothAdapter.getDefaultAdapter()
        //开启笔记
        iv_c.setOnClickListener {
            EventBus.getDefault().post(AppEvent(f_openPen, null))
        }
        //智能笔管理
        bt_manage.setOnClickListener { _ ->
            if (!adapter.isEnabled) {
                adapter.enable()
                return@setOnClickListener
            }
            mRxPermissions!!.requestEach(Manifest.permission.ACCESS_FINE_LOCATION)
                    .subscribe({
                        when {
                            it.granted -> EventBus.getDefault().post(AppEvent(f_penManage, null))
                            it.shouldShowRequestPermissionRationale -> toast("请务必开启位置权限,功能才能正常使用!")
                            else -> toast("请务必开启位置权限,功能才能正常使用!")
                        }
                    }, {
                        toast("开启权限异常,请稍后尝试!")
                    })
        }

        //我的笔记
        bt_mePen.setOnClickListener {
            EventBus.getDefault().post(AppEvent(f_myNote, null))
        }
    }

    override fun onRightClick() {
    }
}